package com.lanou.web.controller;

import com.lanou.entity.Page;
import com.lanou.entity.Person;
import com.lanou.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/person") //窄化路径
public class PersonController {
    @Autowired //注入实体对象
    private PersonService personService;

    @ResponseBody //返回json数据
    @RequestMapping("/list") //路径
    public Page list(Page page) {
        List<Person> persons = personService.findByPage(page);
        long total = personService.countAll();

//        Map<String, Object> map = new HashMap<>();
//        map.put("rows", persons);
//        map.put("total", total);

        page.setRows(persons);
        page.setTotal(total);

        return page;
    }

    @ResponseBody //返回json数据
    @RequestMapping("/findAll") //路径
    public List<Person> list() {
        return personService.findAll();
    }

    @ResponseBody
    @RequestMapping("/add")
    public Person add(Person person) {
        return personService.add(person);
    }
}
